package cn.com.ctbri.geomonitor.controller;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.supermap.data.Datasource;
import com.supermap.data.DatasourceConnectionInfo;
import com.supermap.data.EngineType;
import com.supermap.data.Workspace;
import com.supermap.data.WorkspaceConnectionInfo;
import com.supermap.data.WorkspaceType;
import com.supermap.data.conversion.DataImport;
import com.supermap.data.conversion.ImportMode;
import com.supermap.data.conversion.ImportSettingSHP;




/**
 * 用户查询Controller
 * @author cumtshuping
 *iningjia
 */
@Controller
public class FileController {
	
	 @Resource
	  ServletContext application;
	 
//	 static Workspace workspace = new Workspace();
	 
	/**
	 * 保存文件
	 * @param multipartFile
	 * @return
	 * @throws Exception
	 */
	private boolean saveFile(MultipartFile file) {
		//判断文件是否为空
	if(!file.isEmpty()){
		try {
			String filePath=application.getRealPath("/")+"upload/"+file.getOriginalFilename();
			//转存文件
			file.transferTo(new File(filePath));
			return true;
		 } catch (Exception e) {
			e.printStackTrace();
		 }
	}	
		   return false;
	}
	
	
	//上传shape文件，做冲突线检测
	@RequestMapping("/user/upload2")
	public String upload2(@RequestParam("files") MultipartFile[] files) throws Exception{
		Workspace workspace = new Workspace();
	    DatasourceConnectionInfo datasourceconnection = new DatasourceConnectionInfo();
		
	    //设置MySQL数据源连接需要参数
	    datasourceconnection.setEngineType(EngineType.MYSQL);
	    datasourceconnection.setServer("115.159.73.240");
	    datasourceconnection.setDatabase("kjjctest");
		datasourceconnection.setUser("root");
		datasourceconnection.setPassword("xqx930807");
		datasourceconnection.setAlias("Mysql");
		System.out.println("初始化成功");
		  // 打开数据源
        Datasource datasource = workspace.getDatasources().open(datasourceconnection);
        if (datasource == null) {
                System.out.println("打开数据源失败");
                return null;
        } else {
                System.out.println("数据源打开成功！");
                }
        
	    //判file数组不能为空且长度大于0    
        if(files!=null && files.length>0){
      	    //循环获取file数组中得文件 
      	   for(int i =0;i<files.length;i++){
      		   MultipartFile file = files[i];
      		   //保存文件
      		    saveFile(file);
      		 	//传文件!
      		 
      				try {
      					// 构造DWG导入设置对象
      					ImportSettingSHP importSettingSHP = new ImportSettingSHP();
      					// 设置导入模式
      					importSettingSHP.setImportMode(ImportMode.OVERWRITE);
      					importSettingSHP.setTargetDatasource(datasource);
      					importSettingSHP.setSourceFilePath(application.getRealPath("/")+"upload/"+file.getOriginalFilename());
      					// 构造数据导入类对象
      					DataImport dataImport = new DataImport();
      					// 在导入设置信息集合类对象
      					dataImport.getImportSettings().add(importSettingSHP);
      					// 实现数据的转换
      					dataImport.run();
      					System.out.println("shp导入成功");
      					//datasource.getDatasets().get("test");
      				} catch (Exception e) {
      					System.out.println(e.getMessage());
      				}
      	   }
		    	//重定向
        }
		return "success";
		
		
	}
	
	
	
	
	
	
        @RequestMapping("/user/upload")
		public String  upload(@RequestParam("files") MultipartFile[] files) throws Exception{
        	Workspace workspace = new Workspace();
        	try {
    			// 构造工作空间连接信息
    			WorkspaceConnectionInfo dataExWorkspaceConnectionInfo = new WorkspaceConnectionInfo();
    			// 设置连接信息
    			dataExWorkspaceConnectionInfo.setServer(application.getRealPath("/")+"sources/kjjctest.smwu");
    			dataExWorkspaceConnectionInfo.setType(WorkspaceType.SMWU);
    			// 打开工作空间
    			workspace.open(dataExWorkspaceConnectionInfo);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		System.out.println("初始化成功");
    		
		    //判file数组不能为空且长度大于0    
          if(files!=null && files.length>0){
        	    //循环获取file数组中得文件 
        	   for(int i =0;i<files.length;i++){
        		   MultipartFile file = files[i];
        		   //保存文件
        		    saveFile(file);
        		 	//传文件!
        		 	Datasource desdatasource = workspace.getDatasources().get("115.159.73.240_kjjctest");
        				//desdatasource.getDatasets().deleteAll();
        				if (workspace.getMaps().getCount() == 1) {
        					workspace.getMaps().remove(0);
        					workspace.save();
        				}
        				try {
        					// 构造DWG导入设置对象
        					ImportSettingSHP importSettingSHP = new ImportSettingSHP();
        					// 设置导入模式
        					importSettingSHP.setImportMode(ImportMode.OVERWRITE);
        					importSettingSHP.setTargetDatasource(desdatasource);
        					importSettingSHP.setSourceFilePath(application.getRealPath("/")+"upload/"+file.getOriginalFilename());
        					// 构造数据导入类对象
        					DataImport dataImport = new DataImport();
        					// 在导入设置信息集合类对象
        					dataImport.getImportSettings().add(importSettingSHP);
        					// 实现数据的转换
        					dataImport.run();
        					System.out.println("shp导入成功");
        					desdatasource.getDatasets().get("test");
        				} catch (Exception e) {
        					System.out.println(e.getMessage());
        				}
        	   }
		    	//重定向
          }
		   return "success";
		    }
        
        
        
        
        
       
/*	 private  void initialize() {
		try {
			// 构造工作空间连接信息
			WorkspaceConnectionInfo dataExWorkspaceConnectionInfo = new WorkspaceConnectionInfo();
			// 设置连接信息
			dataExWorkspaceConnectionInfo.setServer(application.getRealPath("/")+"sources/"+"DataExchange.smwu");
			dataExWorkspaceConnectionInfo.setType(WorkspaceType.SMWU);
			// 打开工作空间
			workspace.open(dataExWorkspaceConnectionInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("初始化成功");
	}*/


}